﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoCache.Server.BLL;
using VideoCache.Server.MVCWeb.Attributes;
using VideoCache.Server.Model;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    [ActionAuthorize(AuthorizeType.Administrator)]
    public class RoleController : Controller
    {
        //
        // GET: /Admin/Role/

        public ActionResult Index()
        {
            return View(RoleBLL.FindAll(null, null));
        }


        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(RoleInfo info)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(info.Name) && !string.IsNullOrEmpty(info.DisplayName))
                {
                    RoleBLL.Add(info);

                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("Error", "角色名称和显示名称不能为空。");
                }
            }

            return View(info);
        }

        public ActionResult Edit(int id)
        {
            RoleInfo info = RoleBLL.Find(id);
            if (info == null)
            {
                return HttpNotFound();
            }

            return View(info);
        }

        [HttpPost]
        public ActionResult Edit(RoleInfo info)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(info.Name) && !string.IsNullOrEmpty(info.DisplayName))
                {
                    RoleBLL.Update(info);

                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("Error", "角色名称和显示名称不能为空。");
                }
            }

            return View(info);
        }

        public ActionResult Delete(int id)
        {
            RoleBLL.Delete(id);

            return RedirectToAction("Index");
        }
    }
}